<?php

class Domain51_Service_Amazon_S3_Bucket
{
    private $_connection = null;
    
    public function __construct($config)
    {
        $this->_connection = $config['connection'];
    }
    
    public function put($bucket)
    {   
        // strip out unwanted characters
        $bucket = preg_replace('/[^a-zA-Z0-9_\/-]/', '', $bucket);
        $response = $this->_connection->doRequest('PUT', $bucket);
        
        return $response->getResponseCode() == '200';
    }
    
    /**
     * @todo consider refactoring into a factory so you could set these by 
     * separate methods calls and do an execute() -  think PDO
     */
    public function get($bucket, $parameters = array())
    {
        // strip out unwanted characters
        $bucket = preg_replace('/[^a-zA-Z0-9_\/-]/', '', $bucket);
        
        // todo: create a whitelist of valid parameters
        foreach ($parameters as $param_key => $param_value) {
            $parameters[$param_key] = $param_key . '=' . urlencode($param_value);
        }
        $requestUri = $bucket . 
            (count($parameters) > 0 ? '?' . implode('&', $parameters) : '');
        $response = $this->_connection->doRequest('GET', $requestUri);
        
        return $response->getBody();
    }
    
    /**
     *
     * Delete bucket
     *
     * NOTE: returns true even if the bucket didn't exist
     *
     */
    public function delete($bucket) 
    {
        // strip out unwanted characters
        $bucket = preg_replace('/[^a-zA-Z0-9_\/-]/', '', $bucket);
        
        $response = $this->_connection->doRequest('DELETE', $bucket);
        return $response->getResponseCode() == '204';
    }
}

?>